package controller;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameThread extends Thread {

	private SurfaceHolder surficeHolder;
	private GameBoard gameBoard;
	private volatile boolean isRunning;

	public GameThread(SurfaceHolder holder, GameBoard controller) {
		this.surficeHolder = holder;
		this.gameBoard = controller;
		this.isRunning = true;
	}

	public void setRunning(boolean run) {
		this.isRunning = run;
	}

	@SuppressLint("WrongCall")
	@Override
	public void run() {
		while (isRunning) {
			synchronized (this) {
				if (!surficeHolder.getSurface().isValid()) {
					continue;
				}
				Canvas canvas = surficeHolder.lockCanvas();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				gameBoard.updateMyView(canvas);
				gameBoard.onDraw(canvas);
				surficeHolder.unlockCanvasAndPost(canvas);
			}
		}
	}
}
